package utils

import "fmt"

// Ternary 模拟三元操作符
func Ternary(b bool, trueVal, falseVal interface{}) interface{} {
	if b {
		return trueVal
	}
	return falseVal
}

// // EncodePlayerId 编码得到playerId
// func EncodePlayerId(aid, gid uint32, value int64) int64 {
// 	playerIdStr := fmt.Sprintf("%d%d%d%d%d", len(fmt.Sprint(aid)), len(fmt.Sprint(gid)), aid, gid, value)
// 	return Atoi64(playerIdStr)
// }

// EncodePid 编码得到playerId
func EncodePid(cid, gid int32, value int64) int64 {
	playerIdStr := fmt.Sprintf("%d%d%d%d", cid, len(fmt.Sprint(gid)), gid, value)
	return Atoi64(playerIdStr)
}

// DecodePid 解析playerId,获取cid+gid
func DecodePid(playerId int64) (int32, int32) {
	pStr := I64toA(playerId)
	if len(pStr) < 4 {
		return 0, 0
	}
	cidStr := string(pStr[0:3])
	gidScop := Atoi(string(pStr[3]))
	if len(pStr) < 4+gidScop {
		return 0, 0
	}
	gidStr := string(pStr[4 : 4+gidScop])
	return AtoI32(cidStr), AtoI32(gidStr)
}

// // DecodePlayerId 解析playerId,获取sid+gid
// func DecodePlayerId(playerId int64) (uint32, uint32) {
// 	pStr := I64toA(playerId)
// 	aidScop := Atoi(string(pStr[0]))
// 	gidScop := Atoi(string(pStr[1]))
// 	aidStr := string(pStr[2 : aidScop+2])
// 	gidStr := string(pStr[aidScop+2 : aidScop+2+gidScop])
// 	return AtoUI32(aidStr), AtoUI32(gidStr)
// }

// // GetGameIdByPid 解析playerId,获取gid
// func GetGameIdByPid(playerId int64) int32 {
// 	pStr := I64toA(playerId)
// 	if len(pStr) < 2 {
// 		return 0
// 	}
// 	aidScop := Atoi(string(pStr[0]))
// 	gidScop := Atoi(string(pStr[1]))
// 	if len(pStr) < aidScop+2+gidScop {
// 		return 0
// 	}
// 	gidStr := string(pStr[aidScop+2 : aidScop+2+gidScop])
// 	return AtoI32(gidStr)
// }

// GetGidByPid 解析playerId,获取gid
func GetGidByPid(playerId int64) int32 {
	pStr := I64toA(playerId)
	if len(pStr) < 4 {
		return 0
	}
	gidScop := Atoi(string(pStr[3]))
	if len(pStr) < 4+gidScop {
		return 0
	}
	gidStr := string(pStr[4 : 4+gidScop])
	return AtoI32(gidStr)
}

// GetCidByPid 解析playerId,获取cid
func GetCidByPid(playerId int64) int64 {
	pStr := I64toA(playerId)
	if len(pStr) < 3 {
		return 0
	}
	cidStr := string(pStr[0:3])
	return AtoI64(cidStr)
}

// EncodeId 编码得到id
func EncodeId(gid int32, value int64) int64 {
	playerIdStr := fmt.Sprintf("%d%d%d", len(fmt.Sprint(gid)), gid, value)
	return Atoi64(playerIdStr)
}

// // 流畅:0-300 / 正常:301-1000 / 爆满:大于1000
// func GetGameStatus(status, onLineNum int32) int32 {
// 	if status < 0 {
// 		return status
// 	}
// 	if onLineNum < 0 {
// 		return -1
// 	}
// 	if onLineNum <= 300 {
// 		return 0
// 	}
// 	if onLineNum <= 1000 {
// 		return 1
// 	}
// 	return 2
// }

func GetPercentRate2Str(n1, n2 float64) string {
	if n1 <= 0 || n2 <= 0 {
		return "0.000%"
	}
	return fmt.Sprintf("%.3f%%", NewDecimal(n1).Div(n2).Mul(float64(100)).Float64())
}

func GetRate2Str(n1, n2 float64) string {
	if n1 <= 0 || n2 <= 0 {
		return "0.000"
	}
	return fmt.Sprintf("%.3f", NewDecimal(n1).Div(n2).Float64())
}

// 百分比转化-少 val：100 percent：10 = 90
func ValMulPercentDecrease(val, percentVal int32) int32 {
	if val == 0 || percentVal == 0 {
		return 0
	}
	newPercentVal := 100 - percentVal
	percent := NewDecimal(float64(newPercentVal)).Div(float64(100)).Float64()

	return int32(NewDecimal(float64(val)).Mul(float64(percent)).Float64())
}

// 百分比转化-多 val：100 percent：10 = 110
func ValMulPercentAdd(val, percentVal int32) int32 {
	if val == 0 || percentVal == 0 {
		return 0
	}
	percent := 1 + NewDecimal(float64(percentVal)).Div(float64(100)).Float64()

	return int32(NewDecimal(float64(val)).Mul(float64(percent)).Float64())
}
